package view;

import com.alee.extended.painter.NinePatchIconPainter;
import com.alee.extended.panel.WebOverlay;
import com.alee.laf.label.WebLabel;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.text.WebTextArea;
import model.CommentModel;
import util.FontLoader;
import util.ImageLoader;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Image;
import java.text.SimpleDateFormat;

public class CommentView extends WebPanel {
    private static final long serialVersionUID = -8891849164308469183L;
    private static final SimpleDateFormat formatter = new SimpleDateFormat("h:mm a - MMM d, yyyy");
    
    public CommentModel model;

    public CommentView(CommentModel model) {
        NinePatchIconPainter ninePatchPainter = new NinePatchIconPainter(ImageLoader.getImage("comment.9.png"));
        this.setPainter(ninePatchPainter);
                
        this.setBorder(BorderFactory.createMatteBorder(10, 10, 1, 10, new Color(187, 195, 231)));
        this.setDrawBottom(true);
        this.setMargin(5, 0, 5, 10);
        this.setOpaque(true);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.model = model;
        WebLabel authorView = this.createAuthor();
        WebLabel dateView = this.createDate();
        WebTextArea textView = this.createText();
        
        WebOverlay authorDatePanel = new WebOverlay();
        authorDatePanel.setComponent(authorView);
        authorDatePanel.setOpaque(false);
        authorDatePanel.addOverlay(dateView, SwingConstants.RIGHT, SwingConstants.CENTER);
        authorDatePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        this.add(authorDatePanel);
        
        this.add(textView);
    }
    

    
    /**
     * Create the author of the post
     * @return Label of author
     */
    public WebLabel createAuthor() {
        ImageIcon imgIcon = ImageLoader.getImage("kanye.png");
        Image img = imgIcon.getImage();  
        img = img.getScaledInstance(30,30,  java.awt.Image.SCALE_SMOOTH);  
        imgIcon.setImage(img);
        WebLabel authorLabel = new WebLabel(model.authorName, imgIcon);
        Font osBold = FontLoader.getFont(FontLoader.OPENSANS_BOLD);
        authorLabel.setFont(osBold.deriveFont(14f));
        authorLabel.setForeground(new Color(243, 117, 102));
        authorLabel.setMargin(0, 8, 0, 8);
        return authorLabel;
    }
    
    
    /**
     * Create the text of the post.
     * @return Text area
     */
    public WebTextArea createText() {
        Font osRegular = FontLoader.getFont(FontLoader.OPENSANS_REGULAR);
        WebTextArea text = new WebTextArea(model.text);
        text.setEditable(false);
        text.setCursor(null);
        text.setOpaque(false);
        text.setFocusable(false);
        text.setWrapStyleWord(true);
        text.setLineWrap(true);
        text.setFont(osRegular.deriveFont(12f));
        text.setForeground(new Color(60, 60, 60));
        text.setAlignmentX(Component.LEFT_ALIGNMENT);
        text.setMargin(0, 43, 0, 8);
        return text;
    }
    
    
    /**
     * Create the date 
     * @return Date label
     */
    public WebLabel createDate() {
        Font osRegular = FontLoader.getFont(FontLoader.OPENSANS_REGULAR);
        WebLabel timeLabel = new WebLabel(formatter.format(model.date));
        timeLabel.setFont(osRegular.deriveFont(12f));
        timeLabel.setForeground(new Color(140, 140, 140));
        return timeLabel;
    }
}
